package com.hkj.genealogy.genealogydemo.service;

import com.hkj.genealogy.genealogydemo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: genealogydemo
 * @description: 通过数据库校验校验用户信息
 * @author: Hekejian
 * @time: 2020-07-05 01:07
 **/
@Service
public class MyUserDetailsService implements UserDetailsService {
    @Autowired
    UserServiceImp userServiceImp;
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        User userInfo = userServiceImp.getUserInfo(s);
        List<SimpleGrantedAuthority> authorities=new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority(userInfo.getRole()));
        return new org.springframework.security.core.userdetails.User(s,userInfo.getPassword(),authorities);
    }
}
